package com.controller.user;

import com.entity.User;
import com.service.user.AuthCodeService;
import com.service.user.LogService;
import com.service.user.UserEnterService;
import com.service.user.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/** 响应登录页面的控制器 */
@RestController
@CrossOrigin
public class EnterController {

  @Autowired AuthCodeService authCodeService;

  @Autowired LogService logService;

  @Autowired UserEnterService userEnterService;

  @Autowired UserService userService;

  /**
   * 手机号是否注册过
   *
   * @param phoneNumber 手机号，之前需验证手机号的格式正确
   * @return 注册过返回1，没注册返回0
   */
  @GetMapping("enter/isPhoneRegistered")
  public int isPhoneRegistered(@RequestParam("phoneNumber") String phoneNumber) {
    return userEnterService.isPhoneRegistered(phoneNumber);
  }

  /**
   * 用户名是否注册过
   *
   * @param userName 用户名，之前需验证用户名的格式正确
   * @return 注册过返回1，没注册返回0
   */
  @GetMapping("enter/isNameRegistered")
  public int isNameRegistered(@RequestParam("userName") String userName) {
    return userEnterService.isNameRegistered(userName);
  }

  /**
   * 发送并返回验证码
   *
   * @param phoneNumber 需发送验证码的手机号
   * @return 发送成功返回验证码，失败返回0
   */
  @PostMapping("enter/getAuthCode")
  public int getAuthCode(@RequestParam("phoneNumber") String phoneNumber) {
    return authCodeService.sendAuthCodeToVerify(phoneNumber);
  }

  /**
   * 注册，同时添加注册记录
   *
   * @param phoneNumber 手机号
   * @param userName 用户名
   * @param pswd 密码
   * @return 注册成功返回用户编号，失败返回0
   */
  @PostMapping("enter/register")
  public String register(
      @RequestParam("phoneNumber") String phoneNumber,
      @RequestParam("userName") String userName,
      @RequestParam("pswd") String pswd) {
    String userId = userEnterService.register(phoneNumber, userName, pswd);
    logService.addARegisterLog(userId);
    return userId;
  }

  /**
   * 用户直接根据手机号登录（之前已经发送并验证过验证码正确)，若登录成功添加登录记录
   *
   * @param httpServletRequest http请求
   * @param phoneNumber 手机号
   * @return 登陆成功返回用户信息，失败返回null
   */
  @PostMapping("enter/login")
  public User login(
      HttpServletRequest httpServletRequest, @RequestParam("phoneNumber") String phoneNumber) {
    String userId = userEnterService.directlyLogin(phoneNumber);
    if (userId != null) {
      logService.userCome(userId, httpServletRequest);
      return userService.getPersonalInfo(userId);
    }
    return null;
  }

  /**
   * 用户根据手机号/用户名、密码登录（之前已经验证过密码格式正确)，若登录成功添加登录记录
   *
   * @param httpServletRequest http请求
   * @param account 手机号或用户名
   * @return 登陆成功返回用户信息，失败返回null
   */
  @PostMapping("enter/tryLogin")
  public User tryLogin(
      HttpServletRequest httpServletRequest,
      @RequestParam("account") String account,
      @RequestParam("pswd") String pswd) {
    String userId = userEnterService.tryLogin(account, pswd);
    if (userId != null) {
      logService.userCome(userId, httpServletRequest);
      return userService.getPersonalInfo(userId);
    }
    return null;
  }

  /**
   * 重置用户密码
   *
   * @param phoneNumber 手机号(之前需验证过手机号格式正确)
   * @param newPswd 新密码(需验证过密码格式正确)
   * @return 重置成功返回1，否则返回0
   */
  @PostMapping("enter/resetPswd")
  public int resetPswd(
      @RequestParam("phoneNumber") String phoneNumber, @RequestParam("newPswd") String newPswd) {
    return userEnterService.resetPswd(phoneNumber, newPswd);
  }

  @PostMapping("enter/changePhoneNumber")
  public int changePhoneNumber(
      @RequestParam("userId") String userId,
      @RequestParam("newPhoneNumber") String newPhoneNumber) {
    return userEnterService.changePhoneNumber(userId, newPhoneNumber);
  }
}
